/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysOrganization;
import cn.ibizlab.core.ad.service.SysOrganizationService;
import cn.ibizlab.core.ad.filter.SysOrganizationSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysOrganization] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysOrganizationResource {

    @Autowired
    public SysOrganizationService sysOrganizationService;

    @Autowired
    @Lazy
    public SysOrganizationDTOMapping sysOrganizationDtoMapping;

    @Autowired
    @Lazy
    public SysOrganizationFilterDTOMapping sysOrganizationFilterDtoMapping;


    /**
    * 创建Create 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"组织机构" },  notes = "SysOrganization-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Create-all') or hasPermission(this.sysOrganizationDtoMapping.toDomain(#dto),'uaa-SysOrganization-Create')")
    @PostMapping(value = {"sys_organizations","sys_organizations"})
    public ResponseEntity<SysOrganizationDTO> create
            (@Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        sysOrganizationService.create(domain);
        SysOrganization rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 组织机构
    * 
    *
    * @param id id
    * @return ResponseEntity<SysOrganizationDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"组织机构" },  notes = "SysOrganization-Get ")
    @GetMapping("sys_organizations/{id}")
    public ResponseEntity<SysOrganizationDTO> getById
            (@PathVariable("id") String id) {
        SysOrganization rt = sysOrganizationService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 组织机构
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"组织机构" },  notes = "SysOrganization-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Remove-all') or hasPermission(this.sysOrganizationService.get(#id),'uaa-SysOrganization-Remove')")
    @DeleteMapping("sys_organizations/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysOrganizationService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 组织机构
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysOrganizationDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"组织机构" },  notes = "SysOrganization-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Update-all') or hasPermission(this.sysOrganizationService.get(#id),'uaa-SysOrganization-Update')")
    @VersionCheck(entity = "sysorganization" , versionfield = "updateTime")
    @PutMapping("sys_organizations/{id}")
    public ResponseEntity<SysOrganizationDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        domain.setId(id);
        sysOrganizationService.update(domain);
        SysOrganization rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * All 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "All", tags = {"组织机构" },  notes = "SysOrganization-All ")
    @PostMapping("sys_organizations/all")
    public ResponseEntity<List<SysOrganizationDTO>> all
            (@Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        List<SysOrganization> rt = sysOrganizationService.all(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * Cache_evict 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationDTO>
    */
    @ApiOperation(value = "Cache_evict", tags = {"组织机构" },  notes = "SysOrganization-Cache_evict ")
    @PostMapping("sys_organizations/cache_evict")
    public ResponseEntity<SysOrganizationDTO> cacheEvict
            (@Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        SysOrganization rt = sysOrganizationService.cacheEvict(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"组织机构" },  notes = "SysOrganization-CheckKey ")
    @PostMapping("sys_organizations/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        Integer rt = sysOrganizationService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * FindOrganizationId 组织机构
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<String>
    */
    @ApiOperation(value = "FindOrganizationId", tags = {"组织机构" },  notes = "SysOrganization-FindOrganizationId ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-FindOrganizationId-all') or hasPermission(this.sysOrganizationDtoMapping.toDomain(#dto),'uaa-SysOrganization-FindOrganizationId')")
    @PostMapping("sys_organizations/{id}/findorganizationid")
    public ResponseEntity<String> findOrganizationIdById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        domain.setId(id);
        String rt = sysOrganizationService.findOrganizationId(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"组织机构" },  notes = "SysOrganization-GetDraft ")
    @GetMapping("sys_organizations/getdraft")
    public ResponseEntity<SysOrganizationDTO> getDraft
            (@SpringQueryMap SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        SysOrganization rt = sysOrganizationService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * Items 组织机构
    * 根据传入的id 查询 id对应单位清单，不传入id时自动取当前用户可见清单
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "Items", tags = {"组织机构" },  notes = "SysOrganization-Items 根据传入的id 查询 id对应单位清单，不传入id时自动取当前用户可见清单")
    @PostMapping("sys_organizations/{id}/items")
    public ResponseEntity<List<SysOrganizationDTO>> itemsById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        domain.setId(id);
        List<SysOrganization> rt = sysOrganizationService.items(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * Options 组织机构
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "Options", tags = {"组织机构" },  notes = "SysOrganization-Options ")
    @PostMapping("sys_organizations/{id}/options")
    public ResponseEntity<List<SysOrganizationDTO>> optionsById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        domain.setId(id);
        List<SysOrganization> rt = sysOrganizationService.options(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * ParentIds 组织机构
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<String>>
    */
    @ApiOperation(value = "ParentIds", tags = {"组织机构" },  notes = "SysOrganization-ParentIds ")
    @PostMapping("sys_organizations/{id}/parentids")
    public ResponseEntity<List<String>> parentIdsById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        domain.setId(id);
        List<String> rt = sysOrganizationService.parentIds(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 保存Save 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"组织机构" },  notes = "SysOrganization-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Save-all') or hasPermission(this.sysOrganizationDtoMapping.toDomain(#dto),'uaa-SysOrganization-Save')")
    @PostMapping("sys_organizations/save")
    public ResponseEntity<SysOrganizationDTO> save
            (@Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        sysOrganizationService.save(domain);
        SysOrganization rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationDtoMapping.toDto(rt));
    }

    /**
    * SubIds 组织机构
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<String>>
    */
    @ApiOperation(value = "SubIds", tags = {"组织机构" },  notes = "SysOrganization-SubIds ")
    @PostMapping("sys_organizations/{id}/subids")
    public ResponseEntity<List<String>> subIdsById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationDTO dto) {
        SysOrganization domain = sysOrganizationDtoMapping.toDomain(dto);
        domain.setId(id);
        List<String> rt = sysOrganizationService.subIds(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 查询FetchDefault 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"组织机构" },  notes = "SysOrganization-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-FetchDefault-all') or hasPermission(#dto,'uaa-SysOrganization-FetchDefault')")
    @PostMapping("sys_organizations/fetchdefault")
    public ResponseEntity<List<SysOrganizationDTO>> fetchDefault
            (@Validated @RequestBody SysOrganizationFilterDTO dto) {
        SysOrganizationSearchContext context = sysOrganizationFilterDtoMapping.toDomain(dto);
        Page<SysOrganization> domains = sysOrganizationService.searchDefault(context) ;
        List<SysOrganizationDTO> list = sysOrganizationDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchMaintenance 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "查询FetchMaintenance", tags = {"组织机构" },  notes = "SysOrganization-FetchMaintenance ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-FetchMaintenance-all') or hasPermission(#dto,'uaa-SysOrganization-FetchMaintenance')")
    @PostMapping("sys_organizations/fetchmaintenance")
    public ResponseEntity<List<SysOrganizationDTO>> fetchMaintenance
            (@Validated @RequestBody SysOrganizationFilterDTO dto) {
        SysOrganizationSearchContext context = sysOrganizationFilterDtoMapping.toDomain(dto);
        Page<SysOrganization> domains = sysOrganizationService.searchMaintenance(context) ;
        List<SysOrganizationDTO> list = sysOrganizationDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchMyOrg 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "查询FetchMyOrg", tags = {"组织机构" },  notes = "SysOrganization-FetchMyOrg ")
    @PostMapping("sys_organizations/fetchmyorg")
    public ResponseEntity<List<SysOrganizationDTO>> fetchMyOrg
            (@Validated @RequestBody SysOrganizationFilterDTO dto) {
        SysOrganizationSearchContext context = sysOrganizationFilterDtoMapping.toDomain(dto);
        Page<SysOrganization> domains = sysOrganizationService.searchMyOrg(context) ;
        List<SysOrganizationDTO> list = sysOrganizationDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchNav 组织机构
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "查询FetchNav", tags = {"组织机构" },  notes = "SysOrganization-FetchNav ")
    @PostMapping("sys_organizations/fetchnav")
    public ResponseEntity<List<SysOrganizationDTO>> fetchNav
            (@Validated @RequestBody SysOrganizationFilterDTO dto) {
        SysOrganizationSearchContext context = sysOrganizationFilterDtoMapping.toDomain(dto);
        Page<SysOrganization> domains = sysOrganizationService.searchNav(context) ;
        List<SysOrganizationDTO> list = sysOrganizationDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchParentOrg 组织机构
    * 自定义代码：实现过滤本级和下级单位
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationDTO>>
    */
    @ApiOperation(value = "查询FetchParentOrg", tags = {"组织机构" },  notes = "SysOrganization-FetchParentOrg 自定义代码：实现过滤本级和下级单位")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-FetchParentOrg-all') or hasPermission(#dto,'uaa-SysOrganization-FetchParentOrg')")
    @PostMapping("sys_organizations/fetchparentorg")
    public ResponseEntity<List<SysOrganizationDTO>> fetchParentOrg
            (@Validated @RequestBody SysOrganizationFilterDTO dto) {
        SysOrganizationSearchContext context = sysOrganizationFilterDtoMapping.toDomain(dto);
        Page<SysOrganization> domains = sysOrganizationService.searchParentOrg(context) ;
        List<SysOrganizationDTO> list = sysOrganizationDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建组织机构
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Create-all')")
    @ApiOperation(value = "批量新建组织机构", tags = {"组织机构" },  notes = "批量新建组织机构")
	@PostMapping("sys_organizations/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysOrganizationDTO> dtos) {
        sysOrganizationService.createBatch(sysOrganizationDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除组织机构
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Remove-all')")
    @ApiOperation(value = "批量删除组织机构", tags = {"组织机构" },  notes = "批量删除组织机构")
	@DeleteMapping("sys_organizations/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysOrganizationService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新组织机构
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Update-all')")
    @ApiOperation(value = "批量更新组织机构", tags = {"组织机构" },  notes = "批量更新组织机构")
	@PutMapping("sys_organizations/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysOrganizationDTO> dtos) {
        sysOrganizationService.updateBatch(sysOrganizationDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存组织机构
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Save-all')")
    @ApiOperation(value = "批量保存组织机构", tags = {"组织机构" },  notes = "批量保存组织机构")
	@PostMapping("sys_organizations/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysOrganizationDTO> dtos) {
        sysOrganizationService.saveBatch(sysOrganizationDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入组织机构
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganization-Save-all')")
    @ApiOperation(value = "批量导入组织机构", tags = {"组织机构" },  notes = "批量导入组织机构")
	@PostMapping("sys_organizations/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysOrganizationDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysOrganizationService.importData(config,ignoreError,sysOrganizationDtoMapping.toDomain(dtos)));
    }

}
